package com.runa.rccprotocol.dto.tiangang;

import com.runa.protocol.dto.IResponseData;
import com.runa.protocol.util.ByteUtil;
import com.runa.protocol.util.DigitalUtil;
import com.runa.rccprotocol.dto.ConcentratorRequestData;
import com.runa.rccprotocol.dto.daiao.DaiAoMeterRequestData;
import com.runa.rccprotocol.dto.kute.KtValveRequestData;
import com.runa.rccprotocol.dto.pusai.PsValveRequestData;

/**
 * @author diandian
 * @Date 2021/11/1 9:53
 */
public class TianGangValveRequestData extends ConcentratorRequestData {
    public TianGangValveRequestData(String address) {
        super(address);
    }

    @Override
    public boolean check(IResponseData data) {
        return true;
    }

    @Override
    public byte[] getData() {
        return requestData;
    }

    protected byte[] requestData;


    public void init() {
        requestData = new byte[]{0x10, 0x40, (byte) 0xFD, 0, 0x16};
        requestData[3] = ByteUtil.sum(requestData, 0, 3);
        setHaveResult(true);
    }

    public void readHeatMeterOf188(String meter) {
        requestData = new byte[]{(byte) 0xfe, (byte) 0xfe, (byte) 0xfe, (byte) 0xfe,
                0x68, 0x20, 0, 0, 0, 0, 0, 0x55, 0x55, 1, 3, 0x1f, (byte) 0x90, 0, 0, 0x16};
        ByteUtil.tenstringToBcd(requestData, meter, 6, false);
        requestData[18] = ByteUtil.sum(requestData, 4, 14);
        setHaveResult(true);
    }

    public void readHeatMeterOfModbus(int index) {
//        requestData = new byte[]{(byte)no,0x03,0x00,0x00,0x00,0x16, (byte) 0x95, (byte) 0xD0};
        requestData = new byte[]{(byte) index, 0x03, 0x00, 0x00, 0x00, 0x16, (byte) 0x95, (byte) 0xD0};//地址固定0x55(85)
//        requestData[0] = (byte) 85;
//        int value = DigitalUtil.calcCrc16(requestData, 0, 6);
        int value = DigitalUtil.calcCrc16(requestData, 0, 6);
        requestData[6] = Integer.valueOf(value & 0xff).byteValue();
        value = value >> 8;
        requestData[7] = Integer.valueOf(value & 0xff).byteValue();
        setHaveResult(true);
    }

    public void readConfig(String config) {
        byte[] bytes = config.getBytes();
        requestData = new byte[bytes.length + 1 + 4 + 2];
        int index = 0;
        requestData[index++] = (byte)0xF8;
        requestData[index++] = (byte)0x01;
        requestData[index++] = (byte)0x12;
        requestData[index++] = (byte)((bytes.length + 1) & 0xff);
        requestData[index++] = (byte)0x00;
        for (byte aByte : bytes) {
            requestData[index++] = aByte;
        }
        requestData[index++] = ByteUtil.sum(requestData, 0, index);
        requestData[index++] = 0x16;
        setHaveResult(true);
    }

    public void readUrtMeter(String meter) {
        requestData = new byte[]{ (byte)0xFE, (byte)0xFE,
                0x68, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x01, 0x03, 0x1f, (byte) 0x90, 0x03, 0x00, 0x16};
        int start = 2;
        ByteUtil.tenstringToBcd(requestData, meter, start + 2, false);
        requestData[start + 14] = ByteUtil.sum(requestData, start, 14);
        setHaveResult(true);
    }


    public void readWkxValve(String serial) {
        requestData = new byte[16];
        int index = 0;
        requestData[index++] = 0x68;
        requestData[index++] = (byte)0x80;

        byte[] bytes = ByteUtil.hexStringToByteFlip(serial);
        for (int i = 0; i < bytes.length; i++) {
            requestData[index+i] = bytes[i];
        }
        index += 7;

        requestData[index++] = 0x01;
        requestData[index++] = 0x03;
        requestData[index++] = (byte)0x90;
        requestData[index++] = 0x1f;
        requestData[index++] = 0x01;
        requestData[index++] = ByteUtil.sum(requestData, 0, index);
        requestData[index++] = 0x16;
        setHaveResult(true);
    }

    /**
     * 选中代傲表
     * @param meter
     */
    public void selectDaiAoMeter(String meter) {
        DaiAoMeterRequestData daiAoMeterRequestData = new DaiAoMeterRequestData();
        requestData = daiAoMeterRequestData.selectDaiAoMeter(meter);
        setHaveResult(true);
    }

    public void unselectDaiAoMeter(String meter) {
        DaiAoMeterRequestData daiAoMeterRequestData = new DaiAoMeterRequestData();
        requestData = daiAoMeterRequestData.unselectDaiAoMeter(meter);
        setHaveResult(true);
    }

    public void readDaiAoMeter(String meter) {
        DaiAoMeterRequestData daiAoMeterRequestData = new DaiAoMeterRequestData();
        requestData = daiAoMeterRequestData.readDaiAoMeter(meter);
        setHaveResult(true);

    }

    public void readHmMeter(String meter) {
        requestData = new byte[]{ (byte)0xFE, (byte)0xFE,
                0x68, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x01, 0x03, 0x1f, (byte) 0x90, 0x03, 0x00, 0x16};
        int start = 2;
        ByteUtil.tenstringToBcd(requestData, meter, start + 2, false);
        requestData[start + 14] = ByteUtil.sum(requestData, start, 14);
        setHaveResult(true);
    }

    public void readMetterMeter(String meter) {
        byte[] meterCmd = new byte[]{ 0x68, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x01, 0x03, (byte) 0x90, 0x1f, 0x00, 0x00, 0x16};
        ByteUtil.tenstringToBcd(meterCmd, meter, 2, true);
        meterCmd[14] = ByteUtil.sum(meterCmd, 0, 14);

        int len = meterCmd.length + 6;
        requestData = new byte[len];
        requestData[0] = (byte) 0xf8;
        requestData[1] = (byte) 0x01;
        requestData[2] = (byte) 0x09;
        requestData[3] = (byte) meterCmd.length;
        for (int i = 0; i < meterCmd.length; i++) {
            requestData[i+4] = meterCmd[i];
        }
        requestData[len - 2] = ByteUtil.sum(requestData, 0, len - 2);
        requestData[len - 1] = (byte) 0x16;
        setHaveResult(true);
    }

    public static void main(String[] args) {
        TianGangValveRequestData tianGangValveRequestData = new TianGangValveRequestData("00821809");
//        tianGangValveRequestData.readModbusValve(101, 1, 1);
//        tianGangValveRequestData.readMetterMeter("24400720");
//        tianGangValveRequestData.readHmMeter("23081702");
//        tianGangValveRequestData.setWkxValveOpen("02407550", 90, true);
//        tianGangValveRequestData.readConfig("MbusMode");
        System.out.println(ByteUtil.byteToHexString(tianGangValveRequestData.getData()));

        tianGangValveRequestData.chooseHouseMeter("23881782");
        System.out.println("天罡TG户表,选择表: " + ByteUtil.byteToHexString(tianGangValveRequestData.getData()));


    }

    public void chooseHouseMeter(String meter) {
//        requestData = newComm byte[]{0x68, 0x0B, 0x0B, 0x68, 0x73, (byte) 0xFD, 0x52, 0, 0, 0, 0, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0x21, 0, 0x16};
//        requestData = newComm byte[]{0x68, 0x20, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x03, 0x1F, (byte) 0x90, 0x00, 0, 0x16};
        requestData = new byte[]{0x68, 0x0B, 0x0B, 0x68, 0x53, (byte) 0xFD, 0x52, 0, 0, 0, 0, (byte) 0x8F, (byte) 0x41, (byte) 0x02, 0x04, 0, 0x16};
        ByteUtil.tenstringToBcd(requestData, meter, 7, false);
        requestData[15] = ByteUtil.sum(requestData, 4, 11);
        setHaveResult(true);
    }

    public void chooseHouseValve(String valve) {
        requestData = new byte[]{0x68, 0x0B, 0x0B, 0x68, 0x73, (byte) 0xFD, 0x52, 0, 0, 0, 0, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0x21, 0, 0x16};
        ByteUtil.tenstringToBcd(requestData, valve, 7, false);
        requestData[15] = ByteUtil.sum(requestData, 4, 11);
        setHaveResult(true);
    }

    public void setWkxValveOpen(String serial, Integer opening, boolean ifLock) {
        requestData = new byte[17];
        int index = 0;
        requestData[index++] = 0x68;
        requestData[index++] = (byte)0x80;

        byte[] bytes = ByteUtil.hexStringToByteFlip(serial);
        for (int i = 0; i < bytes.length; i++) {
            requestData[index+i] = bytes[i];
        }
        index += 7;

        requestData[index++] = 0x04;
        requestData[index++] = 0x04;
        requestData[index++] = (byte)0xA1;
        requestData[index++] = 0x17;
        requestData[index++] = 0x01;

        requestData[index++] = (byte)(opening & 0xff);

        requestData[index++] = ByteUtil.sum(requestData, 0, index);
        requestData[index++] = 0x16;
        setHaveResult(true);
    }

    public void setHouseValveOpen(int open) {
        requestData = new byte[]{0x68, 0x0C, 0x0C, 0x68, 0x53, (byte) 0xFD, 0x51, 0x0F, 0x35, 0x10,
                0x00, 0x21, 0x00, (byte) open, 0x01, (byte) 0xFF, 0, 0x16};
        requestData[16] = ByteUtil.sum(requestData, 4, 12);
        setHaveResult(true);
    }

    public void readHouseter() {
//        10 5B FD 58 16
        requestData = new byte[]{0x10, 0x5B, (byte) 0xFD, 0, 0x16};
        requestData[3] = ByteUtil.sum(requestData, 1, 2);
        setHaveResult(true);
    }

    public void readHouseValve() {
//        10 5B FD 58 16
        requestData = new byte[]{0x10, 0x5B, (byte) 0xFD, 0, 0x16};
        requestData[3] = ByteUtil.sum(requestData, 1, 2);
        setHaveResult(true);
    }

    public void readValveByMBus(String serial) {
        String s = "68 0A 0A 68 53 02 51 0F 18 10 00 00 00 00 00 16";
        byte[] bytes = ByteUtil.hexStringToByte(s);
        byte[] serialBytes = ByteUtil.hexStringToByte(serial);
        System.arraycopy(serialBytes, 0, bytes, 10, 4);
        byte sum = ByteUtil.sum(bytes, 4, 10);
        bytes[bytes.length - 2] = (byte) (sum & 0xff);
    }

    public void destroy() {
        requestData = new byte[]{0x10, 0x40, (byte) 0xFD, 0, 0x16};
        requestData[3] = ByteUtil.sum(requestData, 1, 2);
        setHaveResult(true);
    }


    public void readElectric(String meterNo) {
        requestData = new byte[]{0x68, 0, 0, 0, 0, 0, 0, 0x68, 0x11, 0x04, 0x33, 0x33, 0x33, 0x33, 0, 0x16};
        ByteUtil.tenstringToBcd(requestData, meterNo, 1, false);
        requestData[14] = ByteUtil.sum(requestData, 0, 14);
        setHaveResult(true);
    }


    public void setKtValveOpen(String serial, Integer opening, boolean ifLock) {
        KtValveRequestData ktValveRequestData = new KtValveRequestData();
        requestData = ktValveRequestData.setKtValveOpen(serial, opening, ifLock);
        setHaveResult(true);
    }

    public void readKtValve(String serial) {
        KtValveRequestData ktValveRequestData = new KtValveRequestData();
        requestData = ktValveRequestData.readKtValve(serial);
        setHaveResult(true);
    }


    public void setPsValveMaxOpen(String serial, Integer opening) {
        PsValveRequestData psValveRequestData = new PsValveRequestData();
        requestData = psValveRequestData.setValveMaxOpen(serial, opening);
        setHaveResult(true);
    }

    public void setPsValveOpen(String serial, Integer opening, boolean ifLock) {
        PsValveRequestData psValveRequestData = new PsValveRequestData();
        requestData = psValveRequestData.setValveOpen(serial, opening, ifLock);
        setHaveResult(true);
    }

    public void readPsValve(String serial) {
        PsValveRequestData psValveRequestData = new PsValveRequestData();
        requestData = psValveRequestData.readValve(serial);
        setHaveResult(true);
    }

    public void readTigerValve(String serial) {
        PsValveRequestData psValveRequestData = new PsValveRequestData();
        requestData = psValveRequestData.readValve(serial);
        setHaveResult(true);
    }
}
